# 第03章 GSON
# 1.1 添加依赖
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.4'
1
2
2
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.4</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
# 1.2 对象转换为json
package com.changan.org_json;
import com.google.gson.Gson;
public class GSONCreateSample {
public static void main(String[] args) {
Person wangxiaoer = new Person();
wangxiaoer.setName("王小二");
wangxiaoer.setAge(25.2);
wangxiaoer.setBirthday("1990-08-23");
wangxiaoer.setSchool("蓝翔");
wangxiaoer.setMajor(new String[]{"理发", "挖掘机"});
wangxiaoer.setHas_girfriend(false);
wangxiaoer.setCar(null);
wangxiaoer.setHouse(null);
wangxiaoer.setComment("这是一个注释");
Gson gson = new Gson();
System.out.println(gson.toJson(wangxiaoer));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1.3 自定义json中显示的名称@SerializedName
package com.changan.org_json;
import com.google.gson.annotations.SerializedName;
public class Person {
@SerializedName("person_name")
private String name;
@SerializedName("person_school")
private String school;
private boolean has_girfriend;
private double age;
private Object car;
private Object house;
private String[] major;
private String comment;
private String birthday;
// getter & setter
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 1.4 GsonBuilder个性化定制
# 1.4.1 setPrettyPrinting json美化
import com.google.gson.GsonBuilder;
public class GSONCreateSample {
public static void main(String[] args) {
Person wangxiaoer = new Person();
wangxiaoer.setName("王小二");
wangxiaoer.setAge(25.2);
wangxiaoer.setBirthday("1990-08-23");
wangxiaoer.setSchool("蓝翔");
wangxiaoer.setMajor(new String[]{"理发", "挖掘机"});
wangxiaoer.setHas_girfriend(false);
wangxiaoer.setCar(null);
wangxiaoer.setHouse(null);
wangxiaoer.setComment("这是一个注释");
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
System.out.println(gson.toJson(wangxiaoer));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1.4.2 setFieldNamingStrategy 修改属性名称
使用FieldNamingStrategy()回调函数:个性化定制,灵活定制JSON
package com.changan.org_json;
import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Field;
public class GSONCreateSample {
public static void main(String[] args) {
Person wangxiaoer = new Person();
wangxiaoer.setName("王小二");
wangxiaoer.setAge(25.2);
wangxiaoer.setBirthday("1990-08-23");
wangxiaoer.setSchool("蓝翔");
wangxiaoer.setMajor(new String[]{"理发", "挖掘机"});
wangxiaoer.setHas_girfriend(false);
wangxiaoer.setCar(null);
wangxiaoer.setHouse(null);
wangxiaoer.setComment("这是一个注释");
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
builder.setFieldNamingStrategy(new FieldNamingStrategy() {
@Override
public String translateName(Field f) {
if (f.getName().equals("age")) {
return "person_age";
}
return f.getName();
}
});
Gson gson = builder.create();
System.out.println(gson.toJson(wangxiaoer));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 1.5 使用transient忽略某些属性
在Person中定义transient修饰的属性:
private transient String ignore;
public String getIgnore() {
return ignore;
}
public void setIgnore(String ignore) {
this.ignore = ignore;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 1.6 Gson解析
# 1.6.1 解析JSON文件
GSON解析JSON文件:可以直接生成对象(基于反射机制),约定优于配置
package com.changan.org_json;
import com.google.gson.Gson;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class GsonReadSample {
public static void main(String[] args) throws IOException {
File file = new File(ReadJsonSample.class.getResource("/wangxiaoer.json").getFile());
String content = FileUtils.readFileToString(file, "UTF-8");
Gson gson = new Gson();
Person person = gson.fromJson(content, Person.class);
System.out.println(person);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1.6.2 解析带日期转换
创建新的PersonWithDate
package com.changan.org_json;
import java.util.Arrays;
import java.util.Date;
public class PersonWithDate {
//@SerializedName("person_name")
private String name;
//@SerializedName("person_school")
private String school;
private boolean has_girfriend;
private double age;
private Object car;
private Object house;
private String[] major;
private String comment;
private Date birthday;
// getter & setter & toString
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
使用GsonBuilder的setDateFormat方法
package com.changan.org_json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class GsonReadSample {
public static void main(String[] args) throws IOException {
File file = new File(ReadJsonSample.class.getResource("/wangxiaoer.json").getFile());
String content = FileUtils.readFileToString(file, "UTF-8");
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd").create();
PersonWithDate person = gson.fromJson(content, PersonWithDate.class);
System.out.println(person.getBirthday().toLocaleString());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 1.6.3 集合类解析
修改PersonWitDate
package com.changan.org_json;
import java.util.Date;
import java.util.List;
public class PersonWithDate {
//@SerializedName("person_name")
private String name;
//@SerializedName("person_school")
private String school;
private boolean has_girfriend;
private double age;
private Object car;
private Object house;
//替换为集合类
private List<String> major;
private String comment;
private Date birthday;
// getter & setter & toString
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.changan.org_json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class GsonReadSample {
public static void main(String[] args) throws IOException {
File file = new File(ReadJsonSample.class.getResource("/wangxiaoer.json").getFile());
String content = FileUtils.readFileToString(file, "UTF-8");
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd").create();
PersonWithDate person = gson.fromJson(content, PersonWithDate.class);
System.out.println(person.getBirthday().toLocaleString());
System.out.println(person.getMajor());
System.out.println(person.getMajor().getClass());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22